#ifndef __MOVEMENT_H__
#define __MOVEMENT_H__

#include <list>
#include <vector>
#include <SDL/SDL.h>
#include "xmlhelpers.h"
#include "config.h"
#include "field.h"
#include "tickable.h"

enum Direction {STOP, UP, DOWN, LEFT, RIGHT, DIRECTION_MAX};
enum Action {ACT_STOP, ACT_WALK, ACT_DIG, ACT_MAX};

inline int GetDirDx (enum Direction dir) {
	return (dir == LEFT) ? -1 : (dir == RIGHT) ? 1 : 0;
}

inline int GetDirDy (enum Direction dir) {
	return (dir == UP) ? -1 : (dir == DOWN) ? 1 : 0;
}

struct ActionAnimation {
	int frameTime;
	int framesCount;
	int textureRow;
};

class Moveable : Tickable {
	public:
		Moveable(TickableList * tList);
		virtual ~Moveable();

		void HandleTick (Field & field, unsigned int timestamp);
		void SetDirection(Direction dir);
		Texel & GetTexelInFront (Field & field, Direction dir);
		virtual void HandleWallStrike();

		void GetDefaultDstRect (SDL_Rect & rect);
		void GetDefaultSrcRect (SDL_Rect & rect, unsigned time);
		virtual void Draw(SDL_Surface * screen, unsigned int timestamp) = 0;

		void LoadAnimation (rapidxml::xml_node<> * anim_node);
		int x;
		int y;
	protected:
		unsigned int timeStamp;
		Action action;
		Direction currentDir;
		Direction plannedDir;
		int movState;
		int speed;
		int digSpeed;
		int digDamage;
		TickableList * list;

		std::vector<ActionAnimation> actAnimation;
		void LoadActionAnimation (rapidxml::xml_node<> * anim_node, 
					const std::string & actName, Action actType);

		void HandleDirectionSwitch (Field & field);
		void Move (Field & field, unsigned & timeLeft);
		void Dig (Field & field, unsigned & timeLeft);
		void Stop (unsigned & timeLeft, unsigned now);
};

#endif
